<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Landing extends Controller_Index_Base
{    
    public function action_brand()
    {
        $category = $this->request->param('category') ? $this->request->param('category') : null;
        $brand    = $this->request->param('brand') ? $this->request->param('brand') : null;
        
        $content = new View('index/landing/brand_tpl');
        $content->title    = Model_Main::ucfirst_utf8($category).' '.Model_Main::words_ucfirst($brand).' - Stylemay';
        $content->image    = $this->_get_landing_image($category);
        $content->category = $category;
        $content->brand    = $brand; 
        
        $this->response->body($content);
    }
    
    public function action_brand_go()
    {
        $category = $this->request->param('category') ? $this->request->param('category') : null;
        $brand    = $this->request->param('brand') ? $this->request->param('brand') : null;
        
        $keyword  = $brand;
        if ($category) {
            $keyword = $category.' '.$brand;    
        }
        
        $url_patterns = array(
            'http://ad.admitad.com/goto/1d9ed345ddea57d2938cdc28f2033d/?subid=%subaccount%&ulp=%url%',
            'http://ad.admitad.com/goto/3f2779c2d4ea57d2938c4e8640d77b/?subid=%subaccount%&ulp=%url%',
        );
        $search_urls  = array(
            'http://www.wildberries.ru/catalog/0/search.aspx?search=%search%',
            'http://www.lamoda.ru/catalogsearch/result/?q=%search%',
        );
        
        $url = $this->_get_url($keyword, $search_urls, $url_patterns);
        
        header("Location: ".$url);
        die(); 
    }
    
    protected function _get_url($keyword, $shop_urls, $url_patterns)
    {
        $rand     = mt_rand(0, count($shop_urls)-1);
        $shop_url = $shop_urls[$rand];
        $url_pattern = $url_patterns[$rand];
        
        $keyword  = str_replace(' ', '%20', $keyword);
        $shop_url = str_replace('%search%', $keyword, $shop_url);
        
        $subid = null;
        if (isset($_COOKIE['subid']) && $_COOKIE['subid']) {
            $subid = $_COOKIE['subid'];
        }
        return str_replace(array('%url%', '%subaccount%'), array($shop_url, $subid), $url_pattern);  
    }
    
    protected function _get_landing_image($category)
    {
        $directory = '/media/img/landing/';
        $image     = $directory.'1.jpg';
        
        $shoes     = array(
            'обувь', 'ботинки', 'сапоги', 'кроссовки', 'кеды', 'туфли', 'бутсы', 'сабо', 'ботильоны', 'унты', 'валенки', 
            'полусапоги', 'шиповки', 'резиновые сапоги', 'шлепанцы', 'угги', 'сланцы', 'мокасины', 'луноходы', 'галоши',
            'босоножки', 'балетки', 
        );
        
        $accessories = array(
            'сумки', 'часы', 'рюкзаки', 'перчатки', 'шапки', 'куклы', 'трусы', 'кошельки', 'зонты', 'духи', 'тушь', 'пояса',
            'очки', 'варежки', 'шарфы', 'шали', 'зеркальца', 'клатчи', 'портфели', 'косметички',     
        );
        
        if (in_array($category, $shoes)) {
            $image = $directory.'2.jpg';   
        } else if (in_array($category, $accessories)) {
            $image = $directory.'3.jpg';    
        }
        
        return $image;
    }
    
    public function action_go()
    {
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;
        $product    = ORM::factory('product', $product_id);
        
        $url = $product->get_url();
        header("Location: ".$url);
        die();
    }
} 
